﻿package core.ui.util
{
    import core.enum.*;
    import core.event.*;
    import core.image.*;
    import core.ui.controls.*;
    import flash.display.*;
    import flash.events.*;
    import flash.geom.*;
    import flash.text.*;

    public class TypeTuyitu extends FloatBox
    {
        var ed:Tuyitu;
        var drag:Sprite;
        var layer:Layer;
        var ok:Button;
        public var text:TextInput;
        public var font:Dropdown;
        public var size:Dropslider;
        public var format:Dropdown;
        public var color:ColorPad;

        public function TypeTuyitu(param1:Tuyitu)
        {
            var _loc_5:int = 0;
            super(240, 245);
            this.ed = param1;
            this.visible = false;
            var _loc_2:* = new Literal("rm_text");
            _loc_2.x = 8;
            _loc_2.y = 15;
            addChild(_loc_2);
            this.text = new TextInput(219, 0, "", 80);
            this.text.t.addEventListener(KeyboardEvent.KEY_UP, this.TextChange, false, 1, true);
            this.text.t.multiline = true;
            this.text.x = 10;
            this.text.y = 33;
            addChild(this.text);
            var _loc_3:* = new Literal("rm_font");
            _loc_3.x = 8;
            _loc_3.y = 120;
            addChild(_loc_3);
            this.font = new Dropdown(220, 220);
            var _loc_4:* = Font.enumerateFonts(true).sortOn("fontName");
            while (_loc_5 < _loc_4.length)
            {
                
                this.font.AddItem(_loc_4[_loc_5].fontName, _loc_4[_loc_5].fontName);
                _loc_5++;
            }
            this.font.Bind();
            this.font.Set("Verdana");
            this.font.addEventListener(SelectEvent.COMMAND, this.FontChange, false, 1, true);
            this.font.addEventListener(SelectEvent.HOVER, this.FontPreview, false, 1, true);
            this.font.addEventListener(SelectEvent.CLOSE, this.FontChange, false, 1, true);
            this.font.x = 10;
            this.font.y = 138;
            var _loc_6:* = new Literal("rm_size");
            _loc_6.x = 8;
            _loc_6.y = 161;
            addChild(_loc_6);
            this.size = new Dropslider(50, 5, 130, 30);
            this.size.addEventListener(SliderEvent.DRAG, this.SizeChange, false, 1, true);
            this.size.x = 10;
            this.size.y = 179;
            var _loc_7:* = new Literal("rm_style");
            _loc_7.x = 73;
            _loc_7.y = 161;
            addChild(_loc_7);
            this.format = new Dropdown(90, 90);
            this.format.x = 75;
            this.format.y = 179;
            this.format.addEventListener(SelectEvent.COMMAND, this.FormatChange, false, 1, true);
            this.format.addEventListener(SelectEvent.HOVER, this.FormatPreview, false, 1, true);
            this.format.addEventListener(SelectEvent.CLOSE, this.FormatChange, false, 1, true);
            this.format.AddItem(U.RM("rm_regular"), "0", true);
            this.format.AddItem(U.RM("rm_bold"), "1");
            this.format.AddItem(U.RM("rm_italic"), "2");
            this.format.AddItem(U.RM("rm_bold") + "/" + U.RM("rm_italic"), "3");
            this.format.Bind();
            var _loc_8:* = new Literal("rm_color");
            _loc_8.x = 178;
            _loc_8.y = 161;
            addChild(_loc_8);
            this.color = new ColorPad(0, true);
            this.color.addEventListener(MouseEvent.MOUSE_DOWN, this.ColorMouseDown, false, 1, true);
            this.color.addEventListener(ColorPadEvent.CHOOSE_COLOR, this.ColorChange, false, 1, true);
            this.color.x = 180;
            this.color.y = 179;
            this.ok = new Button("rm_ok", 70);
            this.ok.addEventListener(MouseEvent.CLICK, this.OKClick, false, 0, true);
            this.ok.x = 158;
            this.ok.y = 212;
            addChild(this.ok);
            addChild(this.format);
            addChild(this.color);
            addChild(this.size);
            addChild(this.font);
            this.drag = new Sprite();
            this.drag.addEventListener(MouseEvent.MOUSE_DOWN, this.DragMouseDown, false, 0, true);
            this.drag.graphics.beginFill(9474192);
            this.drag.graphics.drawRect(0, 0, 240, 20);
            this.drag.graphics.endFill();
            this.drag.alpha = 0;
            addChild(this.drag);
            return;
        }// end function

        public function Show() : void
        {
            var _loc_1:TextFormat = null;
            this.visible = true;
            this.ed.stage.focus = this.text.t;
            this.layer = this.ed.canvas.layer;
            if (this.layer.type == LayerType.Text)
            {
                _loc_1 = this.layer.text.getTextFormat();
                this.size.Set(_loc_1.size as int);
                this.font.Set(_loc_1.font as String);
                this.color.Set(_loc_1.color as uint);
                if (_loc_1.bold && _loc_1.italic)
                {
                    this.format.Set("3");
                }
                else if (_loc_1.italic)
                {
                    this.format.Set("2");
                }
                else if (_loc_1.bold)
                {
                    this.format.Set("1");
                }
                else
                {
                    this.format.Set("0");
                }
                this.text.text = this.layer.text.text != " " ? (this.layer.text.text) : ("");
                this.layer.SetTextEditMode();
            }
            return;
        }// end function

        public function Hide() : void
        {
            this.visible = false;
            if (this.layer.type == LayerType.Text)
            {
                this.layer.RemoveTextEditMode();
            }
            return;
        }// end function

        private function OKClick(event:MouseEvent) : void
        {
            this.ed.tool.CleanUp();
            return;
        }// end function

        public function ColorMouseDown(event:MouseEvent) : void
        {
            new ColorPicker(this.ed, event.target as ColorPad, true);
            return;
        }// end function

        private function TextChange(event:Event) : void
        {
            this.ed.canvas.layer.text.text = this.text.text;
            return;
        }// end function

        private function FontPreview(event:SelectEvent) : void
        {
            var _loc_2:* = this.layer.text.getTextFormat();
            _loc_2.font = event.val;
            this.layer.text.setTextFormat(_loc_2);
            this.layer.text.defaultTextFormat = _loc_2;
            return;
        }// end function

        private function FontChange(event:Event) : void
        {
            var _loc_2:* = this.layer.text.getTextFormat();
            _loc_2.font = this.font.value;
            this.layer.text.setTextFormat(_loc_2);
            this.layer.text.defaultTextFormat = _loc_2;
            return;
        }// end function

        private function FormatPreview(event:SelectEvent) : void
        {
            this.Format(event.val);
            return;
        }// end function

        private function FormatChange(event:Event) : void
        {
            this.Format(this.format.value);
            return;
        }// end function

        private function Format(2:String) : void
        {
            var _loc_2:* = this.layer.text.getTextFormat();
            switch(2)
            {
                case "0":
                {
                    _loc_2.bold = false;
                    _loc_2.italic = false;
                    break;
                }
                case "1":
                {
                    _loc_2.bold = true;
                    _loc_2.italic = false;
                    break;
                }
                case "2":
                {
                    _loc_2.bold = false;
                    _loc_2.italic = true;
                    break;
                }
                case "3":
                {
                    _loc_2.bold = true;
                    _loc_2.italic = true;
                    break;
                }
                default:
                {
                    break;
                }
            }
            this.layer.text.setTextFormat(_loc_2);
            this.layer.text.defaultTextFormat = _loc_2;
            return;
        }// end function

        private function SizeChange(event:Event) : void
        {
            var _loc_2:* = this.layer.text.getTextFormat();
            _loc_2.size = this.size.value;
            this.layer.text.setTextFormat(_loc_2);
            this.layer.text.defaultTextFormat = _loc_2;
            return;
        }// end function

        private function ColorChange(event:Event) : void
        {
            var _loc_2:* = this.layer.text.getTextFormat();
            _loc_2.color = this.color.value;
            this.layer.text.setTextFormat(_loc_2);
            this.layer.text.defaultTextFormat = _loc_2;
            return;
        }// end function

        function DragMouseDown(event:MouseEvent) : void
        {
            this.startDrag(false, new Rectangle((-this.width) / 2, 40, stage.stageWidth, stage.stageHeight + 1000));
            stage.addEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true, 0, true);
            return;
        }// end function

        function DragMouseUp(event:MouseEvent) : void
        {
            this.stopDrag();
            stage.removeEventListener(MouseEvent.MOUSE_UP, this.DragMouseUp, true);
            return;
        }// end function

    }
}
